/**

 @Name：layuiAdmin 主页控制台
 @Author：贤心
 @Site：http://www.layui.com/admin/
 @License：GPL-2

 */


layui.define(function(exports){

  /*
    下面通过 layui.use 分段加载不同的模块，实现不同区域的同时渲染，从而保证视图的快速呈现
  */


  //区块轮播切换
  layui.use(['admin', 'carousel'], function(){
    var $ = layui.$
        ,admin = layui.admin
        ,carousel = layui.carousel
        ,element = layui.element
        ,device = layui.device();

    //轮播切换
    $('.layadmin-carousel').each(function(){
      var othis = $(this);
      carousel.render({
        elem: this
        ,width: '100%'
        ,arrow: 'none'
        ,interval: othis.data('interval')
        ,autoplay: othis.data('autoplay') === true
        ,trigger: (device.ios || device.android) ? 'click' : 'hover'
        ,anim: othis.data('anim')
      });
    });

    element.render('progress');

  });

  //数据概览
  layui.use(['carousel', 'echarts', 'layer', 'jquery'], function(){
    var $ = layui.$
        ,carousel = layui.carousel
        ,echarts = layui.echarts,
        layer = layui.layer;

    var name= [];
    var mycars= [];
    var echartsApp = [], options = [
      //今日流量趋势
      {
        title: {
          text: '近期检测趋势',
          x: 'center',
          textStyle: {
            fontSize: 14
          }
        },
        tooltip : {
          trigger: 'axis'
        },
        legend: {
          data:['','']
        },
        xAxis : [{
          type : 'category',
          boundaryGap : false,
          data: name
        }],
        yAxis : [{
          type : 'value'
        }],
        series : [{
          name:'检测数量',
          type:'line',
          smooth:true,
          itemStyle: {normal: {areaStyle: {type: 'default'}}},
          data: mycars
        }]
      },
        //全国分布图
      {
        title : {
          text: '全国检测分布图',
          subtext: '数据来自用户使用信息'
        },
        tooltip : {
          trigger: 'item'
        },
        dataRange: {
          orient: 'horizontal',
          min: 0,
          max: 5000,
          text:['高','低'],           // 文本，默认为数值文本
          splitNumber:0
        },
        series : [
          {
            name: '全国检测分布',
            type: 'map',
            mapType: 'china',
            mapLocation: {
              x: 'center'
            },
            selectedMode : 'multiple',
            itemStyle:{
              normal:{label:{show:true}},
              emphasis:{label:{show:true}}
            },
            data:[]
          }
        ]
      }

    ]
        ,elemDataView = $('#LAY-index-dataview').children('div')
        ,renderDataView = function(index){
      echartsApp[index] = echarts.init(elemDataView[index], layui.echartsTheme);
      echartsApp[index].setOption(options[index]);
      window.onresize = echartsApp[index].resize;
    };

    $.getJSON("/console/checkNote/rewrite/countForDate", {}, function (rep) {
      $.each(rep.data, function (j, child) {
        name.push(child.toDay);
        mycars.push(child.checkNoteCount);
      });
      //没找到DOM，终止执行
      if(!elemDataView[0]) return;
      renderDataView(0);
    });

    $.getJSON("/console/checkNote/rewrite/getAddressCount", {}, function (rep) {
      options[1].series[0].data = rep.data;
      options[1].dataRange.max = rep.data[31].value; // 此为返回的集合中的value最大值
      if(!elemDataView[1]) return;
      renderDataView(1);
    });

    //监听数据概览轮播
    var carouselIndex = 0;
    carousel.on('change(LAY-index-dataview)', function(obj){
      console.log(obj.index);
      renderDataView(carouselIndex = obj.index);
    });

    //监听侧边伸缩
    layui.admin.on('side', function(){
      setTimeout(function(){
        renderDataView(carouselIndex);
      }, 300);
    });

    //监听路由
    layui.admin.on('hash(tab)', function(){
      layui.router().path.join('') || renderDataView(carouselIndex);
    });
  });

  //最新订单
  layui.use('table', function(){
    var $ = layui.$
        ,table = layui.table;

    //今日热搜
    table.render({
      elem: '#LAY-index-topSearch'
      ,url: layui.setter.base + 'json/console/top-search.js' //模拟接口
      ,page: true
      ,cols: [[
        {type: 'numbers', fixed: 'left'}
        ,{field: 'keywords', title: '关键词', minWidth: 300, templet: '<div><a href="https://www.baidu.com/s?wd={{ d.keywords }}" target="_blank" class="layui-table-link">{{ d.keywords }}</div>'}
        ,{field: 'frequency', title: '搜索次数', minWidth: 120, sort: true}
        ,{field: 'userNums', title: '用户数', sort: true}
      ]]
      ,skin: 'line'
    });

    //今日热贴
    table.render({
      elem: '#LAY-index-topCard'
      ,url: layui.setter.base + 'json/console/top-card.js' //模拟接口
      ,page: true
      ,cellMinWidth: 120
      ,cols: [[
        {type: 'numbers', fixed: 'left'}
        ,{field: 'title', title: '标题', minWidth: 300, templet: '<div><a href="{{ d.href }}" target="_blank" class="layui-table-link">{{ d.title }}</div>'}
        ,{field: 'username', title: '发帖者'}
        ,{field: 'channel', title: '类别'}
        ,{field: 'crt', title: '点击率', sort: true}
      ]]
      ,skin: 'line'
    });
  });

  exports('home', {})
});